<cfset pID = Request.ID>
<cfset sdate = Request.rdate>
 
<CFSET dattype = DayOfWeekAsString( DayOfWeek(sdate) )>
<cfset sdate = CreateODBCDate(sdate)>


<cfquery name="getSimulated" datasource="#client.company_dsn#" maxrows="1">
	SELECT WH.CATEGORY,WH.PATTERNCODE
    FROM CINWSHISTORY WH, CINWSFILE WF
    
    WHERE 
    	WF.DOCNUMBER = WH.WSDOCNUMBER AND
    	WH.REFERENCEDATE = <cfqueryparam cfsqltype="cf_sql_date" value="#sdate#"> AND
    	WH.PERSONNELIDNO = '#pID#' AND
        WH.ISLEAVE = 'N' AND
        (WF.APPROVED IS NULL OR WF.APPROVED ='N')
    ORDER BY WH.DATELASTUPDATE DESC, WH.TIMELASTUPDATE DESC
</cfquery>


<cfif not getSimulated.RecordCount>
    <cfquery name="getSubTempCat" datasource="#client.company_dsn#" maxrows="1">
        SELECT CATEGORY,PATTERNCODE
        FROM CINWSSUBSKED
        WHERE PERSONNELIDNO = '#pID#'AND
        REFERENCEDATE = <cfqueryparam cfsqltype="cf_sql_date" value="#sdate#">
    </cfquery>
    
    <cfif getSubTempCat.CATEGORY NEQ 'ALT'><!---STR,FLX,ACT--->
        <cfquery name="getSubTemp" datasource="#CLIENT.COMPANY_DSN#" >
            SELECT DESCRIPTION, DAYTYPE, PATTERNCODE AS WSCODE, CATEGORY
            FROM CLKWSPATTERN 
            WHERE PATTERNCODE = '#getSubTempCat.PATTERNCODE#'
        </cfquery>
    <cfelse><!---ALT--->
        <cfquery name="getSubTempAlt" datasource="#CLIENT.COMPANY_DSN#" >
            SELECT WORKSKEDCODE
            FROM CLKWSALTPATTERN 
            PATTERNCODE = '#getSubTempCat.PATTERNCODE#'
        </cfquery>
        
        <cfquery name="getSubTemp" datasource="#CLIENT.COMPANY_DSN#" >
            SELECT DESCRIPTION, #dattype# AS DAYTYPE, WORKSKEDCODE AS WSCODE, CATEGORY
            FROM  CLKWORKSKED  
            WHERE WORKSKEDCODE = '#getSubTempAlt.WORKSKEDCODE#'
        </cfquery>            
    </cfif>
    
    
    <cfif NOT getSubTemp.RecordCount>
        
        <cfquery name="getTempCat" datasource="#client.company_dsn#" maxrows="1">
            SELECT CATEGORY,WORKSKEDCODE, STARTDATE
            FROM CINWSTEMPSKED
            WHERE STARTDATE <=  <cfqueryparam cfsqltype="cf_sql_date" value="#sdate#">
            AND ENDDATE >=  <cfqueryparam cfsqltype="cf_sql_date" value="#sdate#">
            AND PERSONNELIDNO = '#pID#'
        </cfquery>
        
        <cfif getSubTempCat.CATEGORY NEQ 'ALT'><!---STR,FLX,ACT--->
            <cfquery name="getTemp" datasource="#CLIENT.COMPANY_DSN#" >
                SELECT DESCRIPTION, #dattype# AS DAYTYPE, WORKSKEDCODE AS WSCODE, CATEGORY
                FROM CLKWORKSKED 
                WHERE WORKSKEDCODE = '#getTempCat.WORKSKEDCODE#'
            </cfquery>
        <cfelse><!---ALT--->
            <cfmodule template="_getAlt.cfm" sdate="#sdate#" startdate="#getTempCat.STARTDATE#" 
                skedcode="#getTempCat.WORKSKEDCODE#">   
            <cfset getTemp = Schedule >
        </cfif>
        
    
        
        <cfif NOT getTemp.RecordCount>
             <cfquery name="getPermanentCat" datasource="#client.company_dsn#" maxrows="1">
                SELECT CATEGORY,WORKSKEDCODE, STARTDATE
                FROM CINWSMASTER 
                WHERE 
                STARTDATE <=  <cfqueryparam cfsqltype="cf_sql_date" value="#sdate#">
                AND ACTIVESKED = 'Y'
                AND PERSONNELIDNO = '#pID#'
            </cfquery>
            
            <cfif getPermanentCat.CATEGORY NEQ 'ALT'><!---STR,FLX,ACT--->
                <cfquery name="getPermanent"  datasource="#client.company_dsn#">
                    SELECT DESCRIPTION, #dattype# AS DAYTYPE, WORKSKEDCODE AS WSCODE, CATEGORY
                    FROM CLKWORKSKED
                    WHERE WORKSKEDCODE = '#getPermanentCat.WORKSKEDCODE#'
                </cfquery>
                
                <cfset caller.skedQry = getPermanent>
            <cfelse><!---ALT--->
                <cfmodule template="_getAlt.cfm" sdate="#sdate#" startdate="#getPermanentCat.STARTDATE#" 
                skedcode="#getPermanentCat.WORKSKEDCODE#">   
                <cfset getPermanent = Schedule >
            </cfif>
            
            
            <cfset caller.skedQry = getPermanent>  
        <cfelse>
            <cfset caller.skedQry = getTemp>
        </cfif>
    <cfelse>
        <cfset caller.skedQry = getSubTemp>	
    </cfif>
<cfelse>
	<cfif getSimulated.CATEGORY NEQ 'ALT'><!---STR,FLX,ACT--->
        <cfquery name="getSimulatedTemp" datasource="#CLIENT.COMPANY_DSN#" >
            SELECT DESCRIPTION, DAYTYPE, PATTERNCODE AS WSCODE, CATEGORY
            FROM CLKWSPATTERN 
            WHERE PATTERNCODE = '#getSimulated.PATTERNCODE#'
        </cfquery>
    <cfelse><!---ALT--->
        <cfquery name="getSimulatedAlt" datasource="#CLIENT.COMPANY_DSN#" >
            SELECT WORKSKEDCODE
            FROM CLKWSALTPATTERN 
            PATTERNCODE = '#getSimulated.PATTERNCODE#'
        </cfquery>
        
        <cfquery name="getSimulatedTemp" datasource="#CLIENT.COMPANY_DSN#" >
            SELECT DESCRIPTION, #dattype# AS DAYTYPE, WORKSKEDCODE AS WSCODE, CATEGORY
            FROM  CLKWORKSKED  
            WHERE WORKSKEDCODE = '#getSubTempAlt.WORKSKEDCODE#'
        </cfquery>            
    </cfif>

    <cfset caller.skedQry = getSimulatedTemp>	
</cfif>






